<?php

function get_item_production_cost( $item_id, $fac_mm, $fac_tm, $fac_ic, $fac_phc, $corp_base_materials )
{
	if( isset( $corp_base_materials[ $item_id ] ) )
	{
		$retval[ 'typeID' ] = $item_id;
		$retval[ 'product_typeID' ] = 0; //this is final item
		$retval[ 'build_cost' ] = $corp_base_materials[ $item_id ]['price'];
		$retval[ 'materials_volume' ] = $corp_base_materials[ $item_id ]['volume'];
		return $retval;
	}

	include 'db.php';

	//we need to get the BPO ID of the BPO that creates item ID and call the bpo function
	//$get_item_bpo_id = $db_eve->prepare( "SELECT blueprintTypeID FROM invBlueprintTypes WHERE productTypeID = ?" );
	$get_item_bpo_id->bind_param( 'i', $item_id );
	$get_item_bpo_id->bind_result( $bpo_id );
	$get_item_bpo_id->execute();
	$get_item_bpo_id->fetch();
	$get_item_bpo_id->close();
	return get_bpo_production_cost( $bpo_id, $fac_mm, $fac_tm, $fac_ic, $fac_phc, $corp_base_materials );
}

function get_bpo_production_cost( $bpo_id, $fac_mm, $fac_tm, $fac_ic, $fac_phc, $corp_base_materials )
{
	include 'db.php';

	//info about single BPO production with given data. It should have typeID, product_typeID, build_cost, materials_volume
	$bpo = array();

	//$get_bpo_levels = $db_corp->prepare( "SELECT ME, PE FROM library BPO WHERE ccp_typeID = ?" );
	//the above is BPO stuff from eve DB we now need ME and PE from our library
	$get_bpo_levels->bind_param( 'i', $bpo_id );
	$get_bpo_levels->bind_result( $bpo_me, $bpo_pe );
	$get_bpo_levels->execute();
	$result = $get_bpo_levels->fetch();
	$get_bpo_levels->close();

	//$get_bpo_details = $db_eve->prepare( "SELECT bpo.productTypeID, inv.typeName, bpo.productionTime, bpo.productivityModifier, bpo.materialModifier, bpo.wasteFactor, inv.portionSize FROM invBlueprintTypes AS bpo INNER JOIN $eve_db.invTypes AS inv ON inv.typeID = bpo.productTypeID WHERE bpo.blueprintTypeID = ?" );
	$get_bpo_details->bind_param( 'i', $bpo_id );
	$get_bpo_details->bind_result( $bpo_product_id, $bpo_product_name, $bpo_production_time, $bpo_productivity_modifier, $bpo_material_modifier, $bpo_waste_factor, $bpo_portion_size );
	$get_bpo_details->execute();
	$get_bpo_details->fetch();
	$get_bpo_details->close();

	if ( NULL == $result )
	{
		//no such BPO exists in our library!
		echo 'No BPO with typeID '.$bpo_id.' in corp library. Will fall-back to Jita buy price! (ATM -1,000,000,000,000 as eve-c linkup doesn\'t work yet.)<br/>';
		$bpo[ 'typeID' ] = $bpo_id;
		$bpo[ 'product_typeID' ] = $bpo_product_id;
		$bpo[ 'build_cost' ] = -1000000000000;
		$bpo[ 'materials_volume' ] = -1000000000000;
		return $bpo;
	}

	//we now have BPO details that we can use to retrieve bill of materials. Base materials first.
	$get_item_base_materials->bind_param( 'i', $bpo_product_id );
	$get_item_base_materials->bind_result( $material_name, $material_id, $material_quantity );
	$get_item_base_materials->execute();
	$base_materials = array();

	while( $get_item_base_materials->fetch() )
	{
		if( isset($base_materials[ $material_id ]) )
		{
			$base_materials[ $material_id ] += $material_quantity;
		}
		else
		{
			$base_materials[ $material_id ] = $material_quantity;
		}
	}

	$get_item_base_materials->close();

	//now we need extra materials
	$get_bpo_extra_materials->bind_param( 'i', $bpo_id );
	$get_bpo_extra_materials->bind_result( $extra_name, $extra_id, $extra_quantity, $extra_recycle );
	$get_bpo_extra_materials->execute();
	$extra_materials = array();
	$extra_materials_recycled = array();
	while( $get_bpo_extra_materials->fetch() )
	{
		if( $extra_recycle != 1 )
		{
			if( isset($extra_materials[ $extra_id ]) )
			{
				$extra_materials[ $extra_id ] += $extra_quantity;
			}
			else
			{
				$extra_materials[ $extra_id ] = $extra_quantity;
			}
		}
		else
		{
			$extra_materials_recycled[] = $extra_id;
		}
	}
	$get_bpo_extra_materials->close();

	//now when we know what is being recycled we need to get the exact bill of materials from recycled stuff
	$reprocessed_materials = array();
	foreach( $extra_materials_recycled AS $recycled_item_id )
	{
		$get_item_reprocess_result->bind_param( 'i', $recycled_item_id );
		$get_item_reprocess_result->bind_result( $recycled_name, $recycled_id, $recycled_quantity );
		$get_item_reprocess_result->execute();

		while( $get_item_reprocess_result->fetch() )
		{
			if( isset( $reprocessed_materials[ $recycled_id ] ) )
			{
				$reprocessed_materials[ $recycled_id ] += $recycled_quantity;
			}
			else
			{
				$reprocessed_materials[ $recycled_id ] = $recycled_quantity;
			}
		}

		$get_item_reprocess_result->close();
	}

	//now when we know the exact amount of recycled materials we need to substract them from base materials
	foreach( $reprocessed_materials as $mat_id => $mat_quantity )
	{
		if( isset( $base_materials[ $mat_id ] ) )
		{
			//we have such base material, we need to subtract the reprocessed qunatity
			$base_materials[ $mat_id ] -= $mat_quantity;
		}
	}

	//now we need to calculate waste level on each base material
	foreach( $base_materials as $material_id => $material_quantity )
	{
		$waste = 0;
		if( $bpo_me >= 0 )
		{
			$waste = round($material_quantity * ( $bpo_waste_factor/100 ) * ( 1/($bpo_me + 1 ) ) );
		}
		else
		{
			$waste = round($material_quantity * ( $bpo_waste_factor/100 ) * ( 1 - $bpo_me) );
		}
		//now that we know waste we need to add it to base materials
		$base_materials[ $material_id ] += $waste;
	}

	//extra materials are not influenced by ME nor PE, nor material modifier in station so we don't touch them. We need to apply facility material modifier to materials though
	foreach( $base_materials as $material_id => $material_quantity )
	{
		$base_materials[ $material_id ] *= $fac_mm;
		//after we have calculate run per batch we can adjust it for single unit
		$base_materials[ $material_id ] /= $bpo_portion_size;
	}

	//now we need to calculate the build cost of the bpo. We assume perfect skills and no implants.
	$production_time_modifier = (1 - (0.04 * 5) ) * 1.0 * $fac_tm;
	$production_time = 0;
	if( $bpo_pe >= 0 )
	{
		$production_time = $bpo_production_time * ( 1 - ($bpo_productivity_modifier/$bpo_production_time)*($bpo_pe/(1+$bpo_pe)) ) * $production_time_modifier;
	}
	else
	{
		$production_time = $bpo_production_time * ( 1 - ($bpo_productivity_modifier/$bpo_production_time)*($bpo_pe-1)) * $production_time_modifier;
	}
	$production_cost = $fac_ic + ($fac_phc / 3600) * $production_time;

	//now we have the exact amount of materials needed - base, extra and reprocessed. Those may be more complicated materials so we will have to descend recursivly
	$bpo[ 'typeID' ] = $bpo_id;
	$bpo[ 'product_typeID' ] = $bpo_product_id;
	$bpo[ 'build_cost' ] = $production_cost;
	$bpo[ 'materials_volume' ] = 0;
	$bpo[ 'materials' ] = array(); //this should contain material_id -> count

	//base materials
	foreach( $base_materials as $mat_id => $mat_quantity )
	{
		$material = get_item_production_cost( $mat_id, $fac_mm, $fac_tm, $fac_ic, $fac_phc, $corp_base_materials );
		$bpo[ 'build_cost' ] += $mat_quantity * $material['build_cost'];
		$bpo[ 'materials_volume' ] += $mat_quantity * $material['materials_volume'];
		if( isset( $material[ 'materials' ] ) )
		{
			foreach( $material[ 'materials' ] as $src_mat => $src_quantity )
			{
				$bpo[ 'materials' ][ $src_mat ] += $src_quantity * $mat_quantity;
			}
		}
		else
		{
			$bpo[ 'materials' ][ $mat_id ] += $mat_quantity;
		}


	}

	//extra materials
	foreach( $extra_materials as $mat_id => $mat_quantity )
	{
		$material = get_item_production_cost( $mat_id, $fac_mm, $fac_tm, $fac_ic, $fac_phc, $corp_base_materials );
		$bpo[ 'build_cost' ] += $mat_quantity * $material['build_cost'];
		$bpo[ 'materials_volume' ] += $mat_quantity * $material['materials_volume'];
		if( isset( $material[ 'materials' ] ) )
		{
			foreach( $material[ 'materials' ] as $src_mat => $src_quantity )
			{
				$bpo[ 'materials' ][ $src_mat ] += $src_quantity * $mat_quantity;
			}
		}
		else
		{
			$bpo[ 'materials' ][ $mat_id ] += $mat_quantity;
		}
	}

	foreach( $reprocessed_materials as $mat_id => $mat_quantity )
	{
		$material = get_item_production_cost( $mat_id, $fac_mm, $fac_tm, $fac_ic, $fac_phc, $corp_base_materials );
		$bpo[ 'build_cost' ] += $mat_quantity * $material['build_cost'];
		$bpo[ 'materials_volume' ] += $mat_quantity * $material['materials_volume'];
		if( isset( $material[ 'materials' ] ) )
		{
			foreach( $material[ 'materials' ] as $src_mat => $src_quantity )
			{
				$bpo[ 'materials' ][ $src_mat ] += $src_quantity * $mat_quantity;
			}
		}
		else
		{
			$bpo[ 'materials' ][ $mat_id ] += $mat_quantity;
		}
	}

	return $bpo;
}

function ceiling($number, $significance = 1)
{
	return ceil($number/$significance)*$significance;
}

?>